<template>
	<view>
		<!-- #ifndef MP-WEIXIN  -->
		<view class="status_bar"><!-- 这里是状态栏 --></view>
		<view class="head">
			<!-- #ifdef APP-PLUS || H5-->
		<!-- 	<image src="../../static/img/back.png" mode="" @click="ret()"></image> -->
			<!-- #endif -->
			<text>设置</text>
		</view>
		<!-- #endif -->
		<view class="setting-box">
			<!-- #ifdef APP-PLUS-->
			<view>
				<!-- <image src="../../static/img/shezhi_banben.png" mode=""></image> -->
				<text>版本</text>
				<text class="right">v{{ type }}</text>
			</view>
			<view @click="pop('clear')">
				<!-- <image src="../../static/img/shezhi_huancun.png" mode=""></image> -->
				<text>清除缓存</text>
				<!-- <text class="right">{{ storage }}</text> -->
			</view>
			<!-- #endif -->
			
			<view @click="rout('feedBack')" v-if="!tourist">
				<!-- <image src="../../static/img/shezhi_yijanfankui.png" mode=""></image> -->
				<text>意见反馈</text>
				<!-- <image src="../../static/img/gengduo_all.png" mode="" class="setting-icon"></image> -->
			</view>

			<view style="border-bottom: none;" @click="pop('out')">
				<!-- <image src="../../static/img/shezhi_tuichu.png" mode=""></image> -->
				<text>{{!tourist?"退出登录":'退出游客登录'}}</text>
				<!-- <image src="../../static/img/gengduo_all.png" mode="" class="setting-icon"></image> -->
			</view>
		</view>
		<view class="foots">
			<text @click="user">《用户协议》</text>
			<text @click="privacy">《隐私协议》</text>
		</view>
		<!-- <uni-popup ref="popup" type="center">
			<view class="popup-box"><rich-text :nodes="text"></rich-text></view>
		</uni-popup> -->
	</view>
</template>

<script>
// import uniPopup from '../../components/uni-popup/uni-popup.vue';
export default {
	data() {
		return {
			type: '',
			storage: '',
			tourist: false,
			text: '',
			prvtext: '',
			usertext: ''
		};
	},
	// components: {
	// 	uniPopup
	// },
	onShareAppMessage(res) {
		let that = this;
		return {
			path: '/pages/index/index'
		};
	},
	onLoad() {
		this.users();
		this.tourist = uni.getStorageSync('tourist');
		console.log(this.tourist);
		// #ifdef APP-PLUS
		let that = this;
		plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
			let version = widgetInfo.version;
			that.type = version;
			let url;
			if (that.tourist) {
				url = '/app/auth/version';
			} else {
				url = '/app/user/version';
			}
			that.$ajax(url, 'get', {
				type: 1
			}).then(res => {
				console.log('版本信息', res);
				if (res.code == 1) {
					if (version != res.data.version) {
						that.edition(res.data.url);
					}
				}
			});
		});
		this.formatSize();
		// #endif
	},
	methods: {
		// 计算缓存
		formatSize() {
			let that = this;
			plus.cache.calculate(function(size) {
				//当前占用的空间大小, 单位：kb
				let sizeCache = parseInt(size);
				if (sizeCache == 0) {
					that.storage = '0B';
				} else if (sizeCache < 1024) {
					that.storage = sizeCache + 'B';
				} else if (sizeCache < 1048576) {
					that.storage = (sizeCache / 1024).toFixed(2) + 'KB';
				} else if (sizeCache < 1073741824) {
					that.storage = (sizeCache / 1048576).toFixed(2) + 'MB';
				} else {
					that.storage = (sizeCache / 1073741824).toFixed(2) + 'GB';
				}
			});
		},
		// 清除缓存
		clear() {
			let that = this;
			let os = plus.os.name;
			if (os == 'Android') {
				let main = plus.android.runtimeMainActivity();
				let sdRoot = main.getCacheDir();
				let files = plus.android.invoke(sdRoot, 'listFiles');
				let len = files.length;
				for (let i = 0; i < len; i++) {
					let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
					plus.io.resolveLocalFileSystemURL(
						filePath,
						function(entry) {
							if (entry.isDirectory) {
								entry.removeRecursively(
									function(entry) {
										//递归删除其下的所有文件及子目录
										uni.showToast({
											title: '缓存清理完成',
											duration: 2000
										});
										that.formatSize(); // 重新计算缓存
									},
									function(e) {
										console.log(e.message);
									}
								);
							} else {
								entry.remove();
							}
						},
						function(e) {
							console.log('文件路径读取失败');
						}
					);
				}
			} else {
				// ios暂时未找到清理缓存的方法，以下是官方提供的方法，但是无效，会报错
				plus.cache.clear(function() {
					uni.showToast({
						title: '缓存清理完成',
						duration: 2000
					});
					that.formatSize();
				});
			}
		},
		// 返回
		ret() {
			uni.navigateBack();
		},
		//页面跳转
		rout(url) {
			uni.navigateTo({
				url: url
			});
		},
		// 版本控制
		getVersions() {
			let that = this;
			// #ifdef APP-PLUS
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				let version = widgetInfo.version;
				that.type = version;
				that.edition(version);
			});
			// #endif
		},
		//安装更新
		edition(url) {
			console.log('当前版本号', url);
			uni.showLoading({
				title: '已有新版本,升级中...',
				mask: true
			});
			const downloadTask = uni.downloadFile({
				url: url,
				success: downloadResult => {
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(
							downloadResult.tempFilePath,
							{},
							function() {
								plus.nativeUI.closeWaiting();
								console.log('安装wgt文件成功！');
								plus.nativeUI.alert('应用资源更新完成！', function() {
									plus.runtime.restart();
								});
								uni.hideLoading();
							},
							function(e) {
								plus.nativeUI.closeWaiting();
								console.log('安装wgt文件失败[' + e.code + ']：' + e.message);
								plus.nativeUI.alert('安装wgt文件失败[' + e.code + ']：' + e.message);
								uni.hideLoading();
							}
						);
					} else {
						plus.nativeUI.alert('安装wgt文件失败');
					}
				},
				fail: err => {
					plus.nativeUI.alert('下载wgt文件失败');
				}
			});
			downloadTask.onProgressUpdate(function(res) {
				console.log('下载进度' + res.progress);
				console.log('已经下载的数据长度' + res.totalBytesWritten);
				console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
			});
		},
		//确认弹窗
		pop(type) {
			let that = this;
			let title = '';
			if (type == 'out') {
				title = '确认要退出吗';
			} else {
				title = '确认清理缓存吗';
			}
			uni.showModal({
				content: title,
				success: function(res) {
					if (res.confirm) {
						if (type == 'out') {
							that.out();
						} else {
							that.clear();
						}
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		// 退出登录
		out() {
			if (uni.getStorageSync('tourist')) {
				uni.removeStorageSync('tourist');
				uni.removeStorageSync('bycartoken');
				uni.showToast({
					title: '退出游客登录',
					icon: 'none',
					mask: false,
					duration: 1500
				});
				setTimeout(() => {
					uni.reLaunch({
						url: '../login/login'
					});
				}, 1200);
				return;
			}
	
			this.$ajax('/app/auth/quit', 'get', {}).then(res => {
				if (res.code == 1) {
					uni.removeStorageSync('bycartoken');
					uni.showToast({
						title: '退出登录成功',
						icon: 'none',
						mask: false,
						duration: 1500
					});
					setTimeout(() => {
						uni.reLaunch({
							url: '../login/login'
						});
					}, 1000);
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						mask: false,
						duration: 1500
					});
				}
			});
		},
		users() {
			this.$ajax('/app/auth/getlogin', 'get').then(res => {
				if (res.code == 1) {
					this.usertext = res.data;
				}
			});
			this.$ajax('/app/auth/getprivacy', 'get').then(res => {
				if (res.code == 1) {
					this.prvtext = res.data;
				}
			});
		},
		//用户协议
		user() {
			this.text = this.usertext;
			this.$refs.popup.open();
		},
		privacy() {
			this.text = this.prvtext;
			this.$refs.popup.open();
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f7f7f7;
}
.status_bar {
	height: var(--status-bar-height);
	width: 100%;
}
.head {
	height: 88upx;
	background-color: white;
	display: flex;
	align-items: center;
	image {
		width: 20upx;
		height: 36upx;
		padding: 0 26upx;
	}
	text {
		flex: 1;
		text-align: center;
		font-size: 36upx;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
		padding-right: 72upx;
	}
}
.setting-box {
	display: flex;
	flex-direction: column;
	width: 90%;
	background: rgba(255, 255, 255, 1);
	border-radius: 10upx;
	margin: 20upx auto;
	padding: 0 20upx;
	view {
		position: relative;
		display: flex;
		align-items: center;
		height: 50upx;
		padding: 16upx 0;
		border-bottom: 2upx solid #ededed;
		text {
			font-size: 30upx;
			font-family: PingFang SC;
			font-weight: 400;
			color: rgba(51, 51, 51, 1);
			padding-left: 20upx;
		}
		image {
			width: 36upx;
			height: 38upx;
		}
		.right {
			position: absolute;
			right: 0;
			color: #999999;
		}
		.setting-icon {
			position: absolute;
			right: 0;
			width: 14upx;
			height: 27upx;
		}
	}
}
.foots {
	position: fixed;
	bottom: 50upx;
	left: 0;
	width: 100%;
	display: flex;
	justify-content: center;
	align-items: center;
	color: rgba(242, 165, 0, 1);
	text {
		font-size: 24upx;
		font-weight: 400;
		color: rgba(242, 165, 0, 1);
	}
	.check {
		width: 28upx;
		height: 28upx;
		border-radius: 50%;
		border: 1px solid #f2a500;
		margin-right: 24upx;
	}
	image {
		width: 31upx;
		height: 31upx;
		margin-right: 24upx;
	}
}
.popup-box {
	padding: 20upx 10upx;
	font-size: 30upx;
	text-align: left;
}
</style>
